﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Pessoas_DadosPessoa__popup_ppUpDocRG : PageBase
{
    private int id = 0;
    private const string dbPath = "arq/";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Id"] != null)
            id = Int32.Parse(Request.QueryString["Id"].ToString());

        if (!IsPostBack)
        {
            CarregarEstado(rcbEstado);
            trResultado.Visible = false;
        }
    }

    #region Eventos
    protected void btnSalvar_Click(object sender, EventArgs e)
    {
        GravarArquivo();
    }
    protected void btnFechar_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('email');", true);
    }
    #endregion

    #region Metodos
    private void CarregarRG()
    {

    }
    private void CarregarEstado(RadComboBox combo)
    {
        try
        {
            Uf uf = new Uf();

            combo.DataTextField = "Sigla";
            combo.DataValueField = "IdUf";
            combo.DataSource = uf.SelectAll();
            combo.DataBind();

            InserirSelecione(combo);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void InserirSelecione(RadComboBox combo)
    {
        try
        {
            combo.Items.Insert(0, new RadComboBoxItem(" -- Selecione -- ", "0"));
            combo.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void GravarArquivo()
    {
        if (RadUpload1.UploadedFiles.Count > 0)
        {
            string nameFile = Salvar();

            foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
            {
                string targetFolder = Server.MapPath("~/Arq");
                validFile.SaveAs(Path.Combine(targetFolder, nameFile), true);
            }

            labelNoResults.Visible = false;
            repeaterResults.Visible = true;
            repeaterResults.DataSource = RadUpload1.UploadedFiles;
            repeaterResults.DataBind();
        }
        else
        {
            labelNoResults.Visible = true;
            repeaterResults.Visible = false;
        }

        trResultado.Visible = true;
        tr1.Visible = false;
        tr2.Visible = false;
        tr3.Visible = false;
        trSeleciona.Visible = false;
        btnSalvar.Visible = false;
        trMsg.Visible = false;

    }
    private string Salvar()
    {
        string name = string.Empty;

        PessoaDocRG rg = new PessoaDocRG();
        rg.IdPessoa = IdPessoa;
        rg.Numero = txtNumero.Text;
        rg.OrgaoEmissor = txtOrgaoEmissor.Text;
        rg.Uf = new Uf(int.Parse(rcbEstado.SelectedValue.ToString()));
        if (txtDataExpedicao.SelectedDate != null)
            rg.DataExpedicao = DateTime.Parse(txtDataExpedicao.SelectedDate.ToString());

        rg.Salvar();

        name = "RG" + IdPessoa.ToString() + rg.IdRg.ToString() + txtNumero.Text + ".pdf";
        rg.Path = name;
        rg.AtualizaPath();

        return name;
    }
    #endregion
}
